﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Entities;
using Sustainalytics.Entities.Governance;

namespace Sustainalytics.Governance.BusinessLogic.Model
{
    public class GovernanceProfileDto
    {

        public static string ComposeFileUrl(Guid id, string host)
        {
            if (host.Contains("+"))
                host = host.Replace("+", "0.0.0.0");

            if (!host.EndsWith("/"))
                host += "/";
            var hostUri = new Uri(host);
            return (new Uri(hostUri, hostUri.LocalPath + "api/v1/files/" + id)).ToString().ToLowerInvariant();
        }

        public static GovernanceProfileDto FromGovernanceProfile(GovernanceCompanyProfile gProfile, string host,GovernancePdfProfileMetadata[] metadatas)
        {
            if (host.Contains("+"))
                host = host.Replace("+", "localhost");

            if (!host.EndsWith("/"))
                host += "/";
            var hostUri = new Uri(host);

            var result = new GovernanceProfileDto()
            {
                Id = gProfile.Id,
                CompanyName = gProfile.CompanyName,
                CompanyNameSafeUrl = gProfile.CompanyNameSafeUrl,
                CreatedDate = gProfile.CreatedDate,
                PublicationDate = gProfile.PublicationDate,
                AnalystView = gProfile.AnalystView,
                FullProfile = gProfile.FullProfile
            };

            if (gProfile.Id != Guid.Empty)
            {
                result.FileId = gProfile.Id;
                result.FileUrl = (new Uri(hostUri, hostUri.LocalPath + "api/v1/files/" + gProfile.Id)).ToString().ToLowerInvariant();
            }

            result.Insights = gProfile.Insights.Select(a => GovernanceInsightDto.FromGovernanceInsight(a, host)).ToList();

            return result;
        }

        public Guid Id { get; set; }
        public string CompanyName { get; set; }
        public string CompanyNameSafeUrl { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime PublicationDate { get; set; }
        public string AnalystView { get; set; }
        public List<GovernanceInsightDto> Insights { get; set; }
        public Guid FileId { get; set; } // profile pdf file id
        public string FileName { get; set; }
        public string FileUrl { get; set; }
        public string ReportType { get; set; }
        public bool FullProfile { get; set; }
    }   
}

